package com.njust.bean;

import com.njust.controller.BookController;
import com.njust.dao.BookDao;
import com.njust.service.BookService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCTest9 {

    private ConfigurableApplicationContext ioc;

    @Before
    public void tearUp(){
        ioc = new ClassPathXmlApplicationContext("config/ioc9.xml");
    }


    /**
     * 实验18：使用@Autowired注解实现根据类型实现自动装配★
     */
    @Test
    public void test18(){
        BookController bookController = ioc.getBean("bookController", BookController.class);
        bookController.doGet1();
    }

    /**
     *实验19：如果资源类型的bean不止一个，
     *         默认根据@Autowired注解标记的成员变量名作为id查找bean，进行装配★
     */
    @Test
    public void test19(){
        BookController bookController = ioc.getBean("bookController", BookController.class);
        bookController.doGet1();
        bookController.doGet2();
    }

    /**
     * 实验20：如果根据成员变量名作为id还是找不到bean，
     *         可以使用@Qualifier注解明确指定目标bean的id★
     */
    @Test
    public void test20(){
        BookController bookController = ioc.getBean("bookController", BookController.class);
        bookController.doGet3();
    }

    /**
     * 实验22：@Autowired注解的required属性指定某个属性允许不被设置
     */
    @Test
    public void test22(){
        BookController bookController = ioc.getBean("bookController", BookController.class);
        bookController.doGet4();
    }

    /**
     * 实验21：在方法的形参位置使用@Qualifier注解
     */
    @Test
    public void test21(){
        System.out.println("容器启动前运行了hahaha方法，hahaha方法的形参上使用了@Qualifier注解");
    }

    /**
     *  @Autowired和@Resource对比
     */
    @Test
    public void test(){
        BookService bookService = ioc.getBean("bookService", BookService.class);
        bookService.test();
    }
}
